package org.jeecg.modules.appuser.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.mall.HttpUtils;
import org.jeecg.modules.appuser.constant.GameUserShandwConstant;
import org.jeecg.modules.appuser.entity.TbUserGameUserShandw;
import org.jeecg.modules.appuser.mapper.TbUserGameUserShandwMapper;
import org.jeecg.modules.appuser.service.ITbUserGameUserShandwService;
import org.jeecg.modules.mall.entity.GameBean;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.jeecg.modules.mall.service.ITbMnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @Description: 闪电玩游戏
 * @Author: jeecg-boot
 * @Date: 2020-05-20
 * @Version: V1.0
 */
@Service
public class TbUserGameUserShandwServiceImpl extends ServiceImpl<TbUserGameUserShandwMapper, TbUserGameUserShandw> implements ITbUserGameUserShandwService {

    @Autowired
    @Lazy(true)
    private ITbMnUserService tbMnUserService;

    String url = "http://platform.shandw.com/getGameInfoList?channelId=13534&sec=\"+time+\"&sign=\"+this.$md5('13534'+time+'9022148b77d242778162c8f07a784307'";
    private static String channelNumer = "9022148b77d242778162c8f07a784307";

    @Override
    public Result<?> getShandwGameList(String user_id) {
        TbMnUser tbMnUser = tbMnUserService.getById(user_id);

        String url = "http://platform.shandw.com/getGameInfoList";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(GameUserShandwConstant.URL);
        stringBuffer.append("?");
        String parma1 = "13534";
        stringBuffer.append("channelId=" + parma1);
        stringBuffer.append("&sec=");
        long param2 = System.currentTimeMillis();
        stringBuffer.append(param2);
        stringBuffer.append("&sign=");
        String md5str = DigestUtils.md5DigestAsHex((parma1 + param2 + channelNumer).getBytes());
        stringBuffer.append(md5str);
        try {
            url = stringBuffer.toString();
            String result = HttpUtils.httpClientGet(url, "");
            JSONObject jsonObject = JSON.parseObject(result);

            String contentlist = jsonObject.getString("list");
            List<GameBean> gameBeanList = JSON.parseArray(contentlist, GameBean.class);
            String iframe_url = "";
            for (GameBean gameBean : gameBeanList) {
                System.out.println(gameBean.getUrl());
                iframe_url = "http://www.shandw.com/auth/";
                iframe_url = iframe_url + "?channel=" + parma1;
                iframe_url = iframe_url + "&openid=" + URLEncoder.encode(user_id, "utf8");
                String nickrandow = RandomUtil.randomNumbers(10);
                String nickname = "口袋" + nickrandow;
                //iframe_url = iframe_url + "&nick=" +nickname;
                iframe_url = iframe_url + "&nick=" + URLEncoder.encode("口袋" + nickrandow, "utf8");
                String avater=tbMnUser.getAvater();
                iframe_url = iframe_url + "&avatar=" + URLEncoder.encode( avater, "utf8");
                iframe_url = iframe_url + "&sex=0";
                iframe_url = iframe_url + "&phone=" + tbMnUser.getPhone();
                long time = new Date().getTime() / 1000;
                iframe_url = iframe_url + "&time=" + time;
                String sign = DigestUtils.md5DigestAsHex(("channel=" + parma1 + "&openid=" + URLEncoder.encode(user_id, "utf8") +"&time=" + time
                        + "&nick="+ nickname +
                        "&avatar=" +avater + "&sex=0&phone=" + tbMnUser.getPhone() + channelNumer).getBytes());
                iframe_url = iframe_url + "&sign=" + sign+"&gid="+gameBean.getId();
                gameBean.setIframeurl(iframe_url);
            }
            return Result.ok(gameBeanList);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
